﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Entity.Common.DataOperation;

namespace WebSite.Admin.CashManager
{
    public partial class UpdateCashApplyStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string FileName = FileUpload1.FileName.ToString();
            string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
            string filename = FileName.Replace(ExtenName, "");
            string newFileName = filename + System.Guid.NewGuid().ToString() + ExtenName;
            string filePath = Server.MapPath("Upload\\") + newFileName;//合并两个路径为上传到服务器上的全路径,其中System.Web.HttpContext.Current.Request.MapPath("~/upload/")将指定的虚拟路径映射到物理路径
            FileUpload1.SaveAs(filePath);
            string cuoinfo="";
            string field = "ID,Status";
            DataTable dt = FileOperation.GetDataListFromExcel(filePath, field, "", ref cuoinfo);
            dt.Columns.Add(new DataColumn("result"));
            if (cuoinfo == "")
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string id = dr["ID"] + "";
                        string status = dr["Status"] + "";
                        string result = "";
                        if (status != "1" && status != "2")
                        {
                            result = "Status值必须为1或2";
                            return;
                        }
                        else
                        {
                            string remark = "";
                            if (status == "1")
                            {
                                remark = "取现成功";
                            }
                            else if (status == "2")
                            {
                                remark = "取现拒绝";
                            }

                            CashApply cainfo = new CashApply();
                            CashApplyHelper cahelper = new CashApplyHelper();
                            cainfo = cahelper.GetCashApply(id);
                            cainfo.Status = Convert.ToInt32(status);
                            string userid = cainfo.UsersID;
                            cainfo.Remark = remark;
                            cainfo.PayDate = System.DateTime.Now;
                            cahelper.EditCashApply(cainfo);

                            if (status == "1")
                            {
                                //付款成功给用户发送消息
                                Message minfo = new Message();
                                MessageHelper mhelper = new MessageHelper();
                                minfo.FromUsersID = "0";
                                minfo.FromNickName = "系统";
                                minfo.ToUsersID = userid;
                                minfo.Flag = 0;
                                minfo.Status = 0;
                                minfo.MessageContent = "提现成功，金额已经打到提现账号中，请注意查收";
                                mhelper.AddMessage(minfo);
                            }
                            //如果是拒绝的话需要将相应的金额插入到对应用户的账户表中
                            if (status == "2")
                            {
                                int applymoney = cainfo.ApplyMoney;
                                
                                CashFlow cfinfo = new CashFlow();
                                CashFlowHelper cfhelper = new CashFlowHelper();
                                cfinfo.UsersID = userid;
                                cfinfo.Money = applymoney;
                                cfinfo.Type = 7;
                                cfinfo.Description = "提现被拒绝" + applymoney.ToString();
                                cfhelper.AddCashFlow(cfinfo);

                                //付款成功给用户发送消息
                                Message minfo = new Message();
                                MessageHelper mhelper = new MessageHelper();
                                minfo.FromUsersID = "0";
                                minfo.FromNickName = "系统";
                                minfo.ToUsersID = userid;
                                minfo.Flag = 0;
                                minfo.Status = 0;
                                minfo.MessageContent = "提现被拒绝";
                                mhelper.AddMessage(minfo);
                            }
                            result = "修改成功";
                        }
                        dr["result"] = result;
                    }

                    //导出结果
                    string[] excelcolumnname = new string[] { "ID", "Status", "Result"};
                    string[] fieldcolumn = new string[] { "ID", "Status", "Result" };

                    string filenameout = "UpdateCashApplyStatusResult_" + System.DateTime.Now;

                    if (!FileOperation.ExportToExcel(Page, filename, dt, excelcolumnname, fieldcolumn, false))
                    {
                        Tools.Alert("导出失败！");
                    }
                }
                else
                {
                    Tools.Alert("数据为空，请重新上传！");
                    return;
                }
            }
            else
            {
                Tools.Alert(cuoinfo);
                return;
            }
        }
        #endregion
    }
}